<template>
    <div>
        <el-form label-width="80px">
            <el-row>
                <el-col :xs="{span:24}" :sm="{span:5}">
                    <el-form-item label="轮播区域">
                        <el-select size="small" v-model="value" placeholder="请选择">
                            <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
               
            </el-row>
            <el-row>
                <el-col :xs="{span:24}" :sm="{span:12}">
                    <el-form-item label="是否启用">
                        <el-switch active-color="#13ce66" v-model="value2" inactive-color="#ff4949"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="{span:24}" :sm="{span:5}">
                    <el-form-item label="轮播图片">
                       <el-upload
                            class="avatar-uploader"
                            action="/api/news/upload"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                     </el-form-item>
                </el-col>
            </el-row>  
            <div class="list-button">
                <el-button type="primary" @click="subBanner()">提交</el-button>
                <router-link to="/manage/scroll">
                    <el-button>取消</el-button>
                </router-link>
            </div>
        </el-form>
    </div>
</template>
<script>
import 	Menu 		    from    "@/service/menu-service.js"
const 	_menu 	        =       new Menu();
export default {
    name:"editBanner",
    data(){
        return{
            bannerList:{
                'bannerId':'',
                'img_url':'',
                'banner_place':'',
                'valid':''
            },
            imageUrl: '',
            value2:true,
            options: [{
                value: '0',
                label: '首页'
                }, {
                value: '1',
                label: '广告区域'
                }
            ],
            value:''
        }
    },
    components: {
    },
    created(){
        // 这里是判断是否是编辑模式，如果有参数的话就是编辑模式，否则就是新增模式
        if(this.$route.params.bannerId){
            this.findBannerById(this.$route.params.bannerId)
        }
    },
    methods:{
        handleAvatarSuccess(res, file) {
            this.imageUrl = file.response.file_path;
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
            this.$message.error('上传图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
            this.$message.error('上传图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        //这是通过ID查找职位
        findBannerById(id){
            const loading = this.$loading({
                lock: true,
                text: '加载中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            _menu.findOneBanner({'id':id}).then((res)=>{
                this.bannerList.bannerId = res.data.id;
                this.value = res.data.banner_place;
                this.value2 = res.data.valid == "0" ?false:true;
                this.imageUrl = res.data.img_url;
                loading.close();
            },(err)=>{
                console.log(err)
            })
        },
        setjobDutyValue(val){
            this.$refs.jobDuty.setValue(val);
        },
        setjobRequireValue(val){
            this.$refs.jobRequire.setValue(val);
        },
        changeJobDuty(val){
            this.jobList.jobDuty = val;
        },
        changeJobRequire(val){
            this.jobList.jobRequire = val;
        },
        subBanner(){
            this.bannerList.valid = this.value2?1:0;
            this.bannerList.img_url = this.imageUrl;
            this.bannerList.banner_place = this.value;
            const loading = this.$loading({
                lock: true,
                text: '保存中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            _menu.subBanner(this.bannerList).then((res)=>{
                if(res.data = 1){
                    this.$router.push({path:'/manage/scroll'});
                }
                loading.close();
            },(err)=>{
                console.log(err);
            })
        }
    }
}
</script>
<style>
@import url("//unpkg.com/element-ui@2.4.11/lib/theme-chalk/index.css");
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    background:#fff;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
.list-button{
    text-align: center;
    margin: 20px;
}
</style>
